import { defineAsyncComponent } from "vue";
import { createRouter, RouteRecordRaw, createWebHashHistory } from "vue-router";
import cookie from "../utils/cookie";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    component: defineAsyncComponent(() => import("../pages/home/index.vue")),
  },
  {
    path: "/login",
    component: defineAsyncComponent(() => import("../pages/login/index.vue")),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, form, next) => {
  if (!cookie.get("token") && to.path !== "/login") {
    next("/login");
  }
  next();
});

export default router;
